#ifndef SALES_DATA_H_INCLUDED
#define SALES_DATA_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class SalesData
{
    friend istream &operator>>(istream &, SalesData &);
    friend ostream &operator<<(ostream &, const SalesData &);
    friend bool operator<(const SalesData &, const SalesData &);
    friend bool operator==(const SalesData &, const SalesData &);

public:
    SalesData() = default;
    SalesData(const string &book) : bookNo(book)
    {
    }
    SalesData(istream &is)
    {
        is >> *this;
    }

public:
    SalesData &operator+=(const SalesData &);
    string get_bn() const
    {
        return this->bookNo;
    }

private:
    string bookNo;
    unsigned unigts_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;
};
inline bool compare_bn(const SalesData &lhs, const SalesData &rhs)
{
    return lhs.get_bn() == rhs.get_bn();
}

SalesData operator+(const SalesData &lhs, const SalesData &rhs);

inline bool operator==(const SalesData &lhs, const SalesData &rhs)
{
    return lhs.unigts_sold == rhs.unigts_sold &&
           lhs.sellingprice == rhs.sellingprice &&
           lhs.saleprice == rhs.saleprice &&
           lhs.get_bn() == rhs.get_bn();
}

inline bool operator!=(const SalesData &lhs, const SalesData &rhs)
{
    return !(lhs == rhs);
}

SalesData &SalesData::operator+=(const SalesData &rhs)
{
    unigts_sold += rhs.unigts_sold;
    saleprice = (rhs.saleprice * rhs.unigts_sold + saleprice * unigts_sold) / (rhs.unigts_sold + unigts_sold);
    if (sellingprice)
    {
        discount = saleprice / sellingprice;
    }
    return *this;
}

SalesData operator+(const SalesData &lhs, const SalesData &rhs)
{
    SalesData ans(lhs);
    ans += rhs;
    return ans;
}

istream &operator>>(istream &is, SalesData &s)
{
    is >> s.bookNo >> s.unigts_sold >> s.sellingprice >> s.saleprice;
    if (is && s.sellingprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = SalesData();
    return is;
}

ostream &operator<<(ostream &out, const SalesData &s)
{
    out << s.get_bn() << " " << s.unigts_sold << " "
        << s.sellingprice << " " << s.saleprice << s.discount;
    return out;
}

#endif
